package orlowski.kamil.simple.app.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import orlowski.kamil.simple.app.domain.Country;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Kamil on 2014-10-16.
 */
public class CountryListAdapter extends ArrayAdapter<Country> implements Filterable {
    private Context context;
    private int layoutResourceId;
    private List<Country> data = null;
    private List<Country> originalData = null;
    private Filter countryFilter;

    public CountryListAdapter(Context context, int layoutResourceId, List<Country> data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
        this.originalData = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Country getItem(int position) {
        return data.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        CountryHolder holder = null;

        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new CountryHolder();
            holder.txtTitle = (TextView) row.findViewById(android.R.id.text1);
            row.setTag(holder);
        } else {
            holder = (CountryHolder) row.getTag();
        }

        Country country = data.get(position);
        holder.txtTitle.setText(country.getCountryName());
        holder.id = country.getCounter();
        return row;
    }

    /* *********************************
    * Holder pattern, it makes the view faster and avoid finding the component
    * **********************************/
    private static class CountryHolder {
        TextView txtTitle;
        int id;
    }

    @Override
    public Filter getFilter() {
        if (countryFilter == null) countryFilter = new PlanetFilter();
        return countryFilter;
    }

    private class PlanetFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            // We implement here the filter logic
            if (constraint == null || constraint.length() == 0) {
                // No filter implemented we return all the list
                results.values = originalData;
                results.count = originalData.size();
            } else {
                // We perform filtering operation
                List<Country> resultFilter = new ArrayList<Country>();
                for (Country country : data) {
                    if (country.getCountryName().toUpperCase().startsWith(constraint.toString().toUpperCase()))
                        resultFilter.add(country);
                }

                results.values = resultFilter;
                results.count = resultFilter.size();
            }
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            // Now we have to inform the adapter about the new list filtered
            if (results.count == 0) notifyDataSetInvalidated();
            else {
                data = (List<Country>) results.values;
                notifyDataSetChanged();
            }
        }
    }
}
